﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class ZBBDao
    {


        public ZBBDao()
        {

            Mapper.CreateMap<Y_ZBB, ZBBModel>();
        }
        public List<Y_ZBB> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_ZBB.Count();

                return edm.Y_ZBB.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public ZBBModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_ZBB.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_ZBB, ZBBModel>(query);
            }

        }


        public int Save(ZBBModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_ZBB.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_ZBB();

                    edm.AddToY_ZBB(query);

                }


                query.ProjectId = obj.ProjectId;

                query.wtdljg = obj.wtdljg;

                query.zbfs = obj.zbfs;

                query.zbfsother = obj.zbfsother;

                query.zbdw = obj.zbdw;

                query.zbtzs = obj.zbtzs;

                query.zbje = obj.zbje;

                query.lybzj = obj.lybzj;

                query.sght = obj.sght;

                query.jldw = obj.jldw;

                query.jlfwf = obj.jlfwf;

                query.gsdw = obj.gsdw;

                query.gsfwf = obj.gsfwf;

                query.sjdw = obj.sjdw;

                query.sjfwf = obj.sjfwf;

                query.kcdw = obj.kcdw;

                query.kcfwf = obj.kcfwf;

                query.zjdw = obj.zjdw;

                query.zxf = obj.zxf;

                query.ghdw = obj.ghdw;

                query.ghht = obj.ghht; 

                query.jllybzj = obj.jllybzj;
                query.gclybzj = obj.gclybzj;
                query.sjlybzj = obj.sjlybzj; 
                query.gsfkfs = obj.gsfkfs;
                query.jlfkfs = obj.jlfkfs;
                query.gcfkfs = obj.gcfkfs; 
                query.sjfkfs = obj.sjfkfs; 
                query.kcfkfs = obj.kcfkfs;
                query.zjfkfs = obj.zjfkfs;
                query.dlhtje = obj.dlhtje;
                query.dlfkfs = obj.dlfkfs;
                query.ghfkfs = obj.ghfkfs;
                query.ghje = obj.ghje;

                query.createuserid = obj.createuserid;

                query.createusername = obj.createusername;

                query.createtime = obj.createtime;
                query.createrealname = obj.createrealname;
                edm.SaveChanges();

                return query.Id;


            }

        }
        public ZBBModel GetOneByProjectId(int id, int reid)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_ZBB.Where(s => s.ProjectId == id&&s.Id==reid);
                 


                return Mapper.Map<Y_ZBB, ZBBModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }


        public List<ZBBModel> GetByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_ZBB.Where(s => s.ProjectId == id); 


                return Mapper.Map<List<Y_ZBB>, List<ZBBModel>>(query.OrderByDescending(s => s.Id).ToList());
            }
        }
        /// <summary>
        /// 获取最后一次修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ZBBModel GetOneByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_ZBB.Where(s => s.ProjectId == id);


                return Mapper.Map<Y_ZBB, ZBBModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_ZBB.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

